/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package OilSimNoteBook;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import sun.misc.Regexp;
import sun.misc.RegexpPool;

class GridCell extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final Color cellColor = Color.WHITE;
    private GridCell[][] board;
    private int row, col;
    private JLabel label;
    private static int innerPanelWidth = 25;
    private static int innerPanelHeight = 25;
    private JPanel environmentalPanel, magneticPanel, gravimetricPanel, waterDepthPanel;
    private Integer waterDepth;
    private String magneticValue;
    private String environmentalValue;
    private String gravimetricValue;

    @Override
    public String toString() {
        return String.format("%d,%s,%s,%s", waterDepth,
                magneticValue, environmentalValue, gravimetricValue);
    }

    public static GridCell fromString(String repr) {
        String[] values = repr.split(",");

        GridCell res = new GridCell(null, 0, 0, 0);
        
        if (!values[0].equals("null")) {
            res.setWaterDepth(Integer.parseInt(values[0]));
        }

        if (!values[1].equals("null")) {
            res.setMagneticValue(values[1]);
        }

        if (!values[2].equals("null")) {
            res.setEnvironmentalValue(values[2]);
        }

        if (!values[3].equals("null")) {
            res.setGravimetricValue(values[3]);
        }
        
        return res;
    }

    public void setWaterDepth(Integer waterDepth) {
        this.waterDepth = waterDepth;
        waterDepthPanel.setToolTipText("Water depth=" + waterDepth.toString());
    }

    public void setMagneticValue(String magneticValue) {
        this.magneticValue = magneticValue;
        magneticPanel.setBackground(Grid.magneticOptionsColors.get(magneticValue));
    }

    public void setEnvironmentalValue(String environmentalValue) {
        this.environmentalValue = environmentalValue;
        environmentalPanel.setBackground(Grid.environmentalOptionsColors.get(environmentalValue));
    }

    public void setGravimetricValue(String gravimetricValue) {
        this.gravimetricValue = gravimetricValue;
        gravimetricPanel.setBackground(Grid.gravimetricOptionsColors.get(gravimetricValue));
    }

    public void setCellNumber(int gridCellNumber) {
        this.label.setText(Integer.valueOf(gridCellNumber).toString());
        this.label.validate();
    }

    public GridCell(GridCell[][] b, int r, int c, int gridCellNumber) {
        this.board = b;
        this.row = r;
        this.col = c;
        this.setPreferredSize(new Dimension(75, 75));
        this.setOpaque(true);
        this.setBackground(Color.BLACK);

        this.label = new JLabel(Integer.valueOf(gridCellNumber).toString());

        GridBagLayout layout = new GridBagLayout();
        this.setLayout(layout);
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        this.add(this.label, gbc);

        magneticPanel = new JPanel();
        magneticPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        magneticPanel.addMouseListener(new MagneticPanelClickListener());
        magneticPanel.setPreferredSize(new Dimension(innerPanelWidth, innerPanelHeight));
        magneticPanel.setBackground(Color.WHITE);
        this.add(magneticPanel, gbc);

        gravimetricPanel = new JPanel();
        gravimetricPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        gravimetricPanel.setPreferredSize(new Dimension(innerPanelWidth, innerPanelHeight));
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gravimetricPanel.addMouseListener(new GravimetricPanelClickListener());
        gravimetricPanel.setBackground(Color.WHITE);
        this.add(gravimetricPanel, gbc);

        environmentalPanel = new JPanel();
        environmentalPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        environmentalPanel.setPreferredSize(new Dimension(innerPanelWidth, innerPanelHeight));
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        environmentalPanel.addMouseListener(new EnvironmentalPanelClickListener());
        environmentalPanel.setBackground(Color.WHITE);
        this.add(environmentalPanel, gbc);

        waterDepthPanel = new JPanel();
        waterDepthPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        waterDepthPanel.setPreferredSize(new Dimension(innerPanelWidth, innerPanelHeight));
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 2;
        waterDepthPanel.addMouseListener(new WaterDepthPanelClickListener());
        waterDepthPanel.setBackground(Color.WHITE);
        this.add(waterDepthPanel, gbc);

        setBorder(BorderFactory.createLineBorder(Color.blue));
    }

    private class MagneticPanelClickListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (Grid.selectedLegend == Grid.magneticKey) {
                final JFrame frame = new JFrame();
                String s = (String) JOptionPane.showInputDialog(
                        frame,
                        "Select Magnetic Option",
                        " Magnetic Dialog Box ",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        Grid.magneticOptions,
                        Grid.magneticOptions[0]);
                magneticValue = s;
                ((JPanel) e.getSource()).setBackground(Grid.magneticOptionsColors.get(magneticValue));
                ((JPanel) e.getSource()).setForeground(Grid.magneticOptionsColors.get(magneticValue));
                ((JPanel) e.getSource()).repaint();
                ((JPanel) e.getSource()).validate();
            }
        }
    }

    private class GravimetricPanelClickListener extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (Grid.selectedLegend == Grid.gravimetricKey) {
                final JFrame frame = new JFrame();
                String s = (String) JOptionPane.showInputDialog(
                        frame,
                        "Select Gravimetric Option",
                        " Gravimetric Dialog Box ",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        Grid.gravimetricOptions,
                        Grid.gravimetricOptions[0]);
                gravimetricValue = s;
                ((JPanel) e.getSource()).setBackground(Grid.gravimetricOptionsColors.get(gravimetricValue));
                ((JPanel) e.getSource()).setForeground(Grid.gravimetricOptionsColors.get(gravimetricValue));
                ((JPanel) e.getSource()).repaint();
                ((JPanel) e.getSource()).validate();
            }
        }
    }

    private class EnvironmentalPanelClickListener extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (Grid.selectedLegend == Grid.environmentalKey) {
                final JFrame frame = new JFrame();
                String s = (String) JOptionPane.showInputDialog(
                        frame,
                        "Select Environmental Option",
                        " Environmental Dialog Box",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        Grid.environmentalOptions,
                        Grid.environmentalOptions[0]);
                environmentalValue = s;
                ((JPanel) e.getSource()).setBackground(Grid.environmentalOptionsColors.get(environmentalValue));
                ((JPanel) e.getSource()).setForeground(Grid.environmentalOptionsColors.get(environmentalValue));
                ((JPanel) e.getSource()).repaint();
                ((JPanel) e.getSource()).validate();
            }
        }
    }

    private class WaterDepthPanelClickListener extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            final JFrame parent = new JFrame();
            String name = JOptionPane.showInputDialog(parent,
                    "Enter water depth", null);
            if (name != null) {
                try {
                    waterDepth = Integer.decode(name);
                    ((JPanel) e.getSource()).setToolTipText("Water depth=" + waterDepth.toString());
                } catch (NumberFormatException exception) {
                    JOptionPane.showMessageDialog(parent, "You entered an invalid number.");
                }
            }
        }
    }

    public void paintComponent(Graphics gr) // does painting of components for my GUI and it is called with the identfier [gr]
    {
        setBackground(cellColor);
        super.paintComponent(gr);
    }
}